<template>
  <div class="container">
    <el-dialog :title="setTitle" :visible.sync="dialogVisible" @close='closeDialog' :close-on-click-modal='false'>
      <el-form :model="dataForm" ref="dataForm" :rules="dataRule" size="mini" label-width="120px" class="common-dialog-height" v-loading="formLoading">
        <el-form-item label="参数键：" prop="paKey">
          <el-input v-model="dataForm.paKey" placeholder="请输入参数Key" :disabled="dialogType!=='add'"></el-input>
        </el-form-item>
        <el-form-item label="参数描述：" prop="paDesc">
          <el-input v-model="dataForm.paDesc" placeholder="请输入参数描述"></el-input>
        </el-form-item>
        <el-form-item label="参数值：" prop="paValue">
          <el-input v-model="dataForm.paValue" placeholder="请输入参数值"></el-input>
        </el-form-item>
<!--        <el-form-item label="排序：" prop="order">-->
<!--          <el-input-number v-model="dataForm.order" controls-position="right" :min="1" :max="999"></el-input-number>-->
<!--          <span> Tips: 数字越小，排序越前</span>-->
<!--        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">关 闭</el-button>
      <el-button type="primary" @click="formDataSubmit()">提 交</el-button>
    </span>
    </el-dialog>
    </div>
</template>
<script>
  export default {
    data () {
      return {
        dialogVisible: false,
        formLoading: false,
        dialogType: '',
        dataForm: {
          paId: '',
          paKey: '',
          paDesc: '',
          paValue: '',
          typeId: ''
          // order: 999
        },
        dataRule: {}

      }
    },
    computed: {
      setTitle() {
        return this.dialogType === 'edit' ? '编辑参数类型' : '新建参数类型'
      }
    },
    methods: {
      init(params) {
        this.dialogVisible = true
        this.dialogType = params.type
        if(this.dialogType === 'edit'){
          this.formLoading = true
          this.dataForm.paId = params.id
          this.$http({
            url: this.$http.adornUrl(`/sys/param/info/${params.id}`),
            method: 'get',
            data: {}
          }).then(response => {
            const result = response.data
            if (result && result.code === 200) {
              const data = result.data
              this.dataForm.paId = data.paId
              this.dataForm.paKey = data.paKey
              this.dataForm.paDesc = data.paDesc
              this.dataForm.paValue = data.paValue
              this.dataForm.typeId = data.typeId
              // this.dataForm.order = data.order
            } else {
              this.$message({message: result.msg, type: 'error'})
            }
            this.formLoading = false
          }).catch(reason => {
            this.$message({message: reason, type: 'error'})
            this.formLoading = false
          })
        } else {
          this.dataForm.typeId = params.pid
        }
      },
      formDataSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const data = this.dataForm
            const tips = (this.dialogType === 'add') ? '新增' : '修改'
            const url = (this.dialogType === 'add') ? this.$http.adornUrl('/sys/param/info') : this.$http.adornUrl(`/sys/param/info/${this.dataForm.id}`)
            const method = (this.dialogType === 'add') ? 'post' : 'put'
            this.$http({
              url: url,
              method: method,
              data: this.$http.adornData(data)
            }).then(response => {
              const result = response.data
              if (result && result.code === 200) {
                this.$emit('refreshData')
                this.dialogVisible = false
                this.$message({message: tips + result.msg, type: 'success'})
              } else {
                this.$message({message: tips + result.msg, type: 'error'})
              }
            }).catch(reason => {
              this.$message({message: reason, type: 'error'})
            })
          }
        })
      },
      closeDialog(){
        this.$refs['dataForm'].resetFields()
        this.dialogType = ''
      }
    }
  }
</script>
<style>
</style>
